﻿using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Red.Scripts.Defines;

namespace Language
{
    internal abstract class OutputPane
    {
        #region Private Data

        private Guid _guid;
        private IVsOutputWindowPane _pane;

        #endregion Private Data

        #region Public Properties

        public Guid Guid => _guid;

        #endregion Public Properties

        #region C-Tor

        public OutputPane( string title )
        {
            Create( title );
        }

        public OutputPane( Guid guid )
        {
            Find( guid );
        }

        #endregion C-Tor

        #region Private Methods

        private static IVsOutputWindow OutputWindow => ServiceProvider.GlobalProvider.GetService( typeof( SVsOutputWindow ) ) as IVsOutputWindow;

        private void Create( string title )
        {
            IVsOutputWindow output = OutputWindow;

            _guid = Guid.NewGuid();
            output.CreatePane( ref _guid, title, Convert.ToInt32( true ), Convert.ToInt32( true ) );
            output.GetPane( ref _guid, out _pane );
        }

        private void Find( Guid guid )
        {
            IVsOutputWindow output = OutputWindow;

            output.GetPane( ref guid, out _pane );

            _guid = ( _pane != null ) ? guid : Guid.Empty;
        }

        #endregion Private Methods

        #region Public Methods

        public void Print( string text )
        {
            _pane?.OutputStringThreadSafe( text );
        }

        public void PrintLine( string text )
        {
            Print( $"{text}\n" );
        }

        public void Activate()
        {
            _pane?.Activate();
        }

        #endregion Public Methods
    }

    #region Implementations

    [Export]
    [Export( "Compiler", typeof( ILogPane ) )]
    internal sealed class CompilerPane : OutputPane, ILogPane
    {
        public CompilerPane() : base( "Red scripts compiler" )
        {
        }
    }

    [Export( "Build", typeof( ILogPane ) )]
    internal sealed class BuildPane : OutputPane, ILogPane
    {
        public BuildPane() : base( VSConstants.BuildOutput )
        //public BuildPane() : base( "Red Scripts Banana" )
        {
        }
    }

    #endregion Implementations
}
